<template>
  <!-- 页头 -->
  <div class="page-header">
    <el-page-header :icon="ArrowLeft" @back="handleBackBtn">
      <template #title>
        <span> 返回 </span>
      </template>
      <template #content>
        <span class="text-large font-600 mr-3"> 委托内容 </span>
      </template>
    </el-page-header>
  </div>
  <!-- 分割线 -->
  <el-divider />
  <!-- 内容 -->
  <div class="page-contene">
    <el-form :model="form" label-width="120px">
      <el-form-item label="委托标题">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="委托类目">
        <el-tree-select v-model="form.tags" :data="data" multiple :render-after-expand="false" show-checkbox
          check-strictly check-on-click-node placeholder="标签" :clearable="true" />
        <!-- <el-select v-model="form.region" placeholder="请选择项目类目">
          <el-option label="Zone one" value="shanghai" />
          <el-option label="Zone two" value="beijing" />
        </el-select> -->
      </el-form-item>
      <el-form-item label="委托金额">
        <el-input v-model="form.amount" onkeypress="value=value.replace(/[^\d]/g,'')"
          :disabled="false" />
      </el-form-item>
      <el-form-item label="委托背景/简介">
        <el-input v-model="form.briefly" type="textarea" />
      </el-form-item>
      <el-form-item label="委托封面">
        <UploadImgCover @updateCover="getCover" :action="upLoadImgCoverUrl"></UploadImgCover>
      </el-form-item>
      <el-form-item label="委托介绍">
        <EditorDiv :value="form.desc" @updateValue="getMsg" :action="upLoadImgUrl"></EditorDiv>
        <!-- <el-input v-model="form.desc" type="textarea" /> -->
      </el-form-item>
      <el-form-item label="上传文件">
        <UploadFileDiv @updateFiles="getFiles" :action="upLoadFileUrl"></UploadFileDiv>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">发布</el-button>
        <el-button @click="handleBackBtn">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, watch } from "vue"
import { useRoute, useRouter } from 'vue-router'
import { ArrowLeft } from '@element-plus/icons-vue'
import { Delete, Download, Plus, ZoomIn } from '@element-plus/icons-vue'
import { genFileId } from 'element-plus'
import type { UploadFile, UploadInstance, UploadProps, UploadRawFile, UploadUserFile } from 'element-plus'
import Service from "@/utils/axios"
import { ElMessage, ElMessageBox } from "element-plus";
import "element-plus/es/components/message/style/css"; // 按需引入ElMessage样式失败，手动引入
import "element-plus/es/components/message-box/style/css";

import EditorDiv from '@/components/QuillEditorDiv/index.vue'
import UploadImgCover from '../UploadImgCover/index.vue'
import UploadFileDiv from '../UploadFile/index.vue'

const route = useRoute()
const router = useRouter()

// 返回上一页
const handleBackBtn = () => {
  router.back();
}

// 委托封面上传的后端地址
let upLoadImgCoverUrl = ref(
  'http://127.0.0.1:3000/api/uploadimgPublishEntCover'
);
// 图片上传的后端地址
let upLoadImgUrl = ref(
  'http://127.0.0.1:3000/api/uploadimgPublishEnt'
);
// 文件上传的后端地址
let upLoadFileUrl = ref(
  'http://127.0.0.1:3000/api/uploadfilePublishEnt'
);


// 上传的封面图片上限
const limitCover = ref(1)
// 上传的封面数量到上限后是否隐藏flag
const noneCoverBtn = ref(false)
// 表单数据
const form = reactive({
  title: '', //项目标题
  tags: '', // 标签
  amount: 0, // 项目金额
  briefly: '', // 项目简介
  fileCoverList: '', // 项目封面
  desc: '',
  files: '',
})

// Cover传参
const getCover = (val: any) => {
  form.fileCoverList = val;
}
// 上传文件getValue
const getFiles = (files: any) => {
  form.files = files;
}

// 复文本框getValue
const getMsg = (val: any) => {
  form.desc = val
}

// 表单提交
const onSubmit = () => {
  let userId = localStorage.getItem('userid');
  console.log('submit!', form);
  Service.post('/bkapi/entrustInfo/addEntrust', {
    form,
    userId
  })
    .then((res: any) => {
      console.log('res', res)
      if (res.data?.status === 200) {
        ElMessage({
          showClose: true,
          message: '发布成功!',
          type: 'success',
        })
      } else {
        ElMessage({
        showClose: true,
        message: res.data?.data,
        type: 'error',
      })
      }
    });
}

// 项目金额输入框-改变时检测是否输入的数字
const handleChange = () => {
  const numRegExp = /^\d+$/;
  console.log(form.amount)
  console.log(numRegExp.test(String(form.amount)));
  // if(numRegExp.test(String(form.amount))) {
  // }
}

// 标签选项数据
const data = [
  {
    value: '前端',
    label: '前端',
    children: [
      {
        value: 'HTML',
        label: 'HTML',
        // children: [
        //   {
        //     value: '1-1-1',
        //     label: 'Level three 1-1-1',
        //   },
        // ],
      },
      {
        value: 'CSS',
        label: 'CSS',
      },
      {
        value: 'JS',
        label: 'JS',
      },
    ],
  },
  {
    value: '2',
    label: 'Level one 2',
    children: [
      {
        value: '2-1',
        label: 'Level two 2-1',
        children: [
          {
            value: '2-1-1',
            label: 'Level three 2-1-1',
          },
        ],
      },
      {
        value: '2-2',
        label: 'Level two 2-2',
        children: [
          {
            value: '2-2-1',
            label: 'Level three 2-2-1',
          },
        ],
      },
    ],
  },
  {
    value: '3',
    label: 'Level one 3',
    children: [
      {
        value: '3-1',
        label: 'Level two 3-1',
        children: [
          {
            value: '3-1-1',
            label: 'Level three 3-1-1',
          },
        ],
      },
      {
        value: '3-2',
        label: 'Level two 3-2',
        children: [
          {
            value: '3-2-1',
            label: 'Level three 3-2-1',
          },
        ],
      },
    ],
  },
]

</script>

<style lang="less" scoped>
.page-contene {
  margin-top: 20px;
  padding: 0 150px;

  :deep(.el-form-item__content) {
    .el-select {
      width: 50%;
    }
  }
}
</style>

<style>
.disUoloadBtn .el-upload--picture-card {
  display: none;
  /* 上传按钮隐藏 */
}
</style>